﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OPLWeb.Models;

namespace OPLWeb.Controllers
{
    public class LeagueController : Controller
    {
        private ORITAEntities db = new ORITAEntities();

        //
        // GET: /League/

        public ActionResult Index(int Id)
        {
            ViewBag.CurrentSeason = Id;
            return View(db.Teams.OrderByDescending(p => p.Average).ToList());
        }

        //
        // GET: /League/Details/5

        public ActionResult Details(long id = 0)
        {
            Team team = db.Teams.Single(t => t.ID == id);
            if (team == null)
            {
                return HttpNotFound();
            }
            return View(team);
        }

        //
        // GET: /League/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /League/Create

        [HttpPost]
        public ActionResult Create(Team team)
        {
            if (ModelState.IsValid)
            {
                db.Teams.AddObject(team);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(team);
        }

        //
        // GET: /League/Edit/5

        public ActionResult Edit(long id = 0)
        {
            Team team = db.Teams.Single(t => t.ID == id);
            if (team == null)
            {
                return HttpNotFound();
            }
            return View(team);
        }

        //
        // POST: /League/Edit/5

        [HttpPost]
        public ActionResult Edit(Team team)
        {
            if (ModelState.IsValid)
            {
                db.Teams.Attach(team);
                db.ObjectStateManager.ChangeObjectState(team, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(team);
        }

        //
        // GET: /League/Delete/5

        public ActionResult Delete(long id = 0)
        {
            Team team = db.Teams.Single(t => t.ID == id);
            if (team == null)
            {
                return HttpNotFound();
            }
            return View(team);
        }

        //
        // POST: /League/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Team team = db.Teams.Single(t => t.ID == id);
            db.Teams.DeleteObject(team);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}